/**
 * Copyright 2008 Adam Ruggles.
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package kiff.util;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.google.inject.AbstractModule;
import com.google.inject.Module;
import com.google.inject.name.Names;

/**
 * Application Loader Module for Google-Guice configuration.
 * @author Adam
 * @version $Id: LoaderModule.java 73 2008-12-02 06:28:25Z a.ruggles $
 * 
 * Created on Oct 12, 2008 at 11:32:18 PM 
 */
public class LoaderModule extends AbstractModule {
    /**
     * The <code>Logger</code> is used by the application to generate a log messages.
     */
    private final Logger logger = LoggerFactory.getLogger(LoaderModule.class);

    /**
     * The path of the settings file.
     */
    private String settings;

    /**
     * Default Constructor.
     */
    public LoaderModule() { }

    /**
     * Constructs a Loader Module and sets the settings file.
     * @param settings The path to the configuration settings properties file.
     */
    public LoaderModule(final String settings) {
    	this.settings = settings;
    }

    /**
	 * Adds a Guice Module by class name.
	 * @param moduleName The module's class name.
	 */
	private void addModule(final String moduleName) {
		if (moduleName == null || moduleName.trim().length() == 0) {
			logger.debug("Cannot add a module with no name");
			return;
		}
		try {
			logger.debug("Attempting to install module [{}]", moduleName);
			Module module = (Module) ClassUtils.getObjectByName(moduleName);
			if (module != null) {
				install(module);
			} else {
				logger.error("Class not found.", moduleName);
			}
		} catch (ClassCastException e) {
			logger.error("Cannot cast [{}] to Module.", moduleName);
		}
	}

	/**
	 * {@inheritDoc}
	 * @see com.google.inject.Module#configure(com.google.inject.Binder)
	 */
	protected void configure() {
		Loader config;
		if (settings == null || settings.trim().length() == 0) {
			 config = new DefaultLoader();
		} else {
			config = new DefaultLoader(settings);
		}
		bind(Loader.class).toInstance(config);
		
		String modules = config.getSetting(String.class, LoaderSetting.MODULES);
		if (modules != null) {
			String[] modulesToLoad = modules.split("\\s*,\\s*");
			for (String module : modulesToLoad) {
				addModule(module);
			}
		}
		// Bind config properties to Guice Names.
		Names.bindProperties(binder(), config.getAsProperties());
	}
}
